package PlugIn.commands;

import java.io.File;
import java.util.Collection;

import javax.swing.JOptionPane;

import FilePanel.Data;
import PlugIn.PlugInModel;

/**
 * @author      Erwan Osouf, Lucas Ennouchi
 * @version     1.0                                
 * @since       29/01/2012
 */

public class Rename extends PlugInModel {
	
	/**
	 * Constructor for the rename command
	 */
	public Rename(Data data) {
		super(data);


	}

	private String result;
	/**
	 * gets the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * 
	 * sets the result
	 * @param result
	 */
	public void setResult(String result) {
		this.result = result;
	}

	
	protected void manageEmpty() {
		setResult("Rien a renomm?");
		
	}
	/**
	 * manages when we are dealing with files
	 */
	protected void manageFile(File f) {
		String inputValue = JOptionPane.showInputDialog("Veuillez rentrer le nouveau nom :");
		String s=f.renameTo(new File(f.getParent()+File.separator+inputValue)) ?
				"a ?t? renomm?" : "n'a pas ?t? renomm?";
		setResult("Le fichier "+s);
	}
	/**
	 * manages when we are dealing with directories
	 */
	protected void manageDir(File f) {
		String inputValue = JOptionPane.showInputDialog("Veuillez rentrer le nouveau nom :");
		String s=f.renameTo(new File(f.getParent()+File.separator+inputValue)) ?
				"a ?t? renomm?" : "n'a pas ?t? renomm?";
		setResult("Le dossier "+s);
	}
	/**
	 * manages when we are dealing with multiple file or directories
	 */
	protected void manageMultiple(Collection<File> lf) {
		boolean b=true;
		String s="";
		for (File f : lf) {
			if (f.isDirectory()) {
				manageDir(f);
				b&=getResult().equals("Le dossier a ?t? renomm?");
			}
			else {
				manageFile(f);
				b&=getResult().equals("Le fichier a ?t? renomm?");
			}
		}
		s=b?"a ?t? renomm?e":"n'a pas ?t? renomm?e";
		setResult("La collection "+s);
		
	}

	@Override
	public String getName() {
		return "Renommer";
	};
	
	

}
